package com.java.module.user.controller.api;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.entity.User;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.utils.AssertKit;
import com.java.module.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.java.common.lang.domain.page.Page;
import com.java.common.lang.domain.page.PageDomain;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.WebKit;
import com.java.module.user.domain.Message;
import com.java.module.user.domain.MessageAssign;
import com.java.module.user.domain.dto.MessageAssignDTO;
import com.java.module.user.domain.vo.MessageAssignVO;
import com.java.module.user.service.IMessageAssignService;
import com.java.module.user.service.IMessageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 基础：消息Controller
 *
 * @author Administrator
 * @date 2021-03-24
 */
@ApiSort(101)
@Api(tags = "消息管理")
@RestController("ApiMessageController")
@RequestMapping("/user/message")
public class MessageController {

    @Autowired
    ApiTokenService apiTokenService;

    @Autowired
    IMessageService messageService;

    @Autowired
    IMessageAssignService messageAssignService;

    @Autowired
    IUserService userService;

    @ApiOperationSupport(order = 2)
    @ApiOperation("消息详情")
    @GetMapping("messageDetails/{mid}")
    @ApiImplicitParam(name = "mid", value = "详情id", dataTypeClass = Long.class, required = true)
    public Message messageDetails(@PathVariable("mid") Long mid) {

        return messageService.selectMessageById(mid);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation("消息列表")
    @GetMapping("/getMessageList")
    public Page<MessageAssignVO> myMessage(MessageAssignDTO messageAssign, @Validated PageDomain page) {
        messageAssign.setUid(apiTokenService.getLoginUserId());
        return WebKit.getPageData(() -> messageAssignService.getMessage(messageAssign));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("设置已读")
    @GetMapping("/changeStatus/{id}")
    @ApiImplicitParam(name = "id", value = "我的消息id", dataTypeClass = Long.class, required = true)
    public int changeStatus(@PathVariable("id") Long id) {
        MessageAssign messageAssign = new MessageAssign();
        messageAssign.setMaid(id);
        messageAssign.setReadStatus(1);
        return messageAssignService.updateMessageAssign(messageAssign);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("删除消息")
    @DeleteMapping("/delMessage/{id}")
    @ApiImplicitParam(name = "id", value = "消息id", dataTypeClass = Long.class, required = true)
    public int delMessage(@PathVariable("id") Long id) {
        return messageAssignService.deleteMessageAssignById(id);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("未读数量")
    @GetMapping("/queryCount")
    public Long queryCount() {
        return messageAssignService.queryUserUnRead();
    }






    /**
     * 新增基础：消息
     */
    @ApiOperationSupport(order = 5)
    @Log(title = "基础：消息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/sendUserMessage")
    public AjaxResult add(@RequestBody Message message) {
        User user = userService.selectUserById(message.getToUid());
        AssertKit.notNull(user, "当前用户不存在");

        User userMine = userService.selectUserById(apiTokenService.getLoginUserId());
        message.setMtype(1);
        message.setFromUid(apiTokenService.getLoginUserId());
        List<Message> list = messageService.selectMessageList(message);
        if (list != null && list.size() > 0) {
            return AjaxResult.success("好友请求已存在");
        }
        message.setTitle(userMine.getNickname()+"向您发送好友请求");
        return WebKit.toAjax(messageService.insertMessageFriend(message));
    }


}